// @ts-ignore
export const recursiveRoutes = (tree: any[], views) => {
    return tree.map((node) => {
        const tempNode = node
        if (tempNode.component) {
            tempNode.component = views[`../pages/indexChild/${tempNode.component}.vue`]
        }
        if (tempNode.children && tempNode.children.length > 0) {
            recursiveRoutes(tempNode.children, views)
        }
        return tempNode
    })
}

// @ts-ignore
export const addRoutes = (store, router) => {
    if (store.userMenu && store.userMenu.length > 0) {
        const routersData = JSON.parse(JSON.stringify(store.userMenu))
        const views = import.meta.glob('../pages/indexChild/**/*.vue')
        recursiveRoutes(routersData, views)
        console.log(routersData)
        // @ts-ignore
        routersData.forEach((data) => {
            addRoute('index', data, router)
        })
    }
}

// @ts-ignore
const addRoute = (parent: string, data: any, router) => {

    if (data.children && data.children.length > 0 && (data.menuType === 'M' || data.menuType === 'C')) {
        if (data.menuType === 'M') {
            router.addRoute(parent, {
                name: data.path,
                path: data.path,
                redirect: data.children[0].path,
                meta: {title: data.menuName}
            })
        } else {
            router.addRoute(parent, {
                name: data.path,
                path: data.path,
                meta: {title: data.menuName}
            })
        }
        // @ts-ignore
        data.children.forEach(child => {
            addRoute(data.path, child, router)
        })
    } else if (data.menuType === 'M' || data.menuType === 'C') {
        router.addRoute(parent, {
            name: data.path,
            path: data.path,
            meta: {title: data.menuName},
            component: data.component
        })
    }

}
